package cn.tedu._03weibo_test.controller;

import cn.tedu._03weibo_test.base.response.JsonResult;
import cn.tedu._03weibo_test.base.response.StatusCode;
import cn.tedu._03weibo_test.mapper.UserMapper;
import cn.tedu._03weibo_test.pojo.dto.UserLoginParam;
import cn.tedu._03weibo_test.pojo.dto.UserRegParam;
import cn.tedu._03weibo_test.pojo.entity.User;
import cn.tedu._03weibo_test.pojo.vo.UserVO;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;
import javax.validation.Valid;
import java.util.Date;
@Validated
@Api(tags = "1.用户管理模块")
@Slf4j
@RequestMapping("/v1/user/")
@RestController
public class UserController {
    @Autowired
    private UserMapper userMapper;
    @ApiOperation("用户注册功能")
    @ApiOperationSupport(order = 10)
    @PostMapping("reg")
    public JsonResult regUser(@Valid @RequestBody  UserRegParam userRegParam){
        log.debug("userregparam = "+userRegParam);
        String username = userRegParam.getUsername();
        int num = userMapper.selectRegUser(username);
        if(num>0){
            return new JsonResult(StatusCode.USERNAME_EXISTS);
        }
        log.debug("userRegParam = {}", userRegParam);
        User user = new User();
        BeanUtils.copyProperties(userRegParam,user);
        user.setCreateTime(new Date());
        int i = userMapper.insertRegUser(user);
        if (i>0){
            return JsonResult.ok(userRegParam);
        }
        return new JsonResult(StatusCode.OPERATION_FAILED);
    }
    @ApiOperation("用户登录功能")
    @ApiOperationSupport(order = 20)
    @PostMapping("login")
    public JsonResult userLogin(@Validated @RequestBody UserLoginParam userLoginParam ,@ApiIgnore HttpSession session){
        log.debug("开始登录："+userLoginParam);
        UserVO userVO = userMapper.selectLoginUser(userLoginParam);
        if (userVO == null){
            return new JsonResult(StatusCode.USERNAME_PASSWORD_ERROR);
        }
        session.setAttribute("user",userVO);
        log.debug("userVO="+userVO);
        return JsonResult.ok(userVO);
    }
    @GetMapping("currentUser")
    @ApiOperation("获取当前用户的信息")
    @ApiOperationSupport(order = 30)
    public JsonResult currentUser(@ApiIgnore HttpSession session){
        log.debug("开始获取用户信息：");
        UserVO userVO = (UserVO) session.getAttribute("user");

        return JsonResult.ok(userVO);
    }
    @GetMapping("logout")
    @ApiOperation("用户退出功能")
    @ApiOperationSupport(order = 40)
    public JsonResult userLogout(@ApiIgnore HttpSession session){
        log.debug("开始退出登录：");
        session.removeAttribute("user");
        return JsonResult.ok();
    }























}
